// This File is part of the "GmailNotifier2" Project
// 
// Copyright  2009 sometmes@gmail.com
// All rights reserved
// 
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using IWshRuntimeLibrary;
using System.Resources;
using System.Reflection;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Collections;
using System.Threading;
using Microsoft.Win32;

namespace Notify2
{
    public partial class SetupUninstallForm : Form
    {
        private string wrongSetupPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "GmailNotifier2");

        public SetupUninstallForm()
        {
            InitializeComponent();
        }

        private void SetupForm_Load(object sender, EventArgs e)
        {
            setupPathTextbox.Text = My.Resources.WillDeleteFiles + "  \"" + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "GmailNotifier2") + "\"";
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void installButton_Click(object sender, EventArgs e)
        {
            try
            {
                SetupForm.ShutdownNotifierProcesses();
                RegistryKey key;
                using (key = Registry.ClassesRoot.OpenSubKey(@"*\shell", true))
                {
                    if (key != null)
                    {
                        string keyname;
                        keyname = "Send To Gmail - Blue";
                        if (key.OpenSubKey(keyname) != null)
                            key.DeleteSubKeyTree(keyname);
                        keyname = "Send To Gmail - Green";
                        if (key.OpenSubKey(keyname) != null)
                            key.DeleteSubKeyTree(keyname);
                        keyname = "Send To Gmail - Pink";
                        if (key.OpenSubKey(keyname) != null)
                            key.DeleteSubKeyTree(keyname);
                    }
                }
                using (key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true))
                {
                    key.DeleteValue("GmailNotifier2-Blue", false);
                    key.DeleteValue("GmailNotifier2-Green", false);
                    key.DeleteValue("GmailNotifier2-Pink", false);
                }
                using (key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall", true))
                {
                    string keyname = "Gmail Notifier 2";
                    if (key.OpenSubKey(keyname) != null)
                        key.DeleteSubKeyTree(keyname);
                }
                SetupUtils.DeleteDirectory(SetupForm.notifierGroup);
                string userconfig = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                userconfig = Path.Combine(userconfig, "sometmes@gmail.com");
                if (Directory.Exists(userconfig))
                {
                    string[] dirs = Directory.GetDirectories(userconfig, "GmailNotifier2.EXE_*");
                    foreach (string dir in dirs)
                    {
                        SetupUtils.DeleteDirectory(dir);
                    }
                }
                SetupUtils.DeleteDirectory(this.wrongSetupPath);
                SetupUtils.DeleteDirectory(SetupForm.setupPath);
                MessageBox.Show("Uninstall finished.", "Uninstall", MessageBoxButtons.OK);
                Application.Exit();
            }
            catch (Exception ex)
            {
                ExceptionMessageBox.Show("Uninstall", ex);
            }
        }
    }
}
